$(function(){
	var category = '';
	var displayPage = 4; //主体页数
	var pageSize = 4;//每页显示的数目
	var init_flag = false;
	
	$(document).on('click', '#js-content', function(e){
	    var target = $(e.target);
	    if(target.hasClass("js-headinfor")){
	    	category=target.attr("data-id");
	    	loadComm();
	    }
	});
	
	//初始化
	loadComm();
	
	var initPager = function(json){
		var num_entries = json.totalCount;
	    //console.log(num_entries);
		$("#pager").show();
	    // 创建分页
	    $("#pager").pagination(num_entries, {
	        num_edge_entries: 2, //边缘页数
	        num_display_entries: displayPage, //主体页数为4
	        callback: pageClick,//回调函数
	        items_per_page: pageSize, //每页显示6项
	        prev_text: "上一页",
	        next_text: "下一页"
	    });
	    
	    //动态添加DOM
        var htmlStr = "", htmlTemp = $("textarea").val();
        
        json.list.forEach(function(obj) {
        	htmlStr += htmlTemp.temp(obj);
        });
        
        //console.log(htmlStr);
        $("#content").html(htmlStr);//将HTML插入到容器中
	};

	var pageClick = function(page_index, jq){
		//console.log(page_index);
		var page = page_index + 1;
		
		if(page == 1 && !init_flag){ //查询第一页并进行分页初始化时，不去服务器再次查询
			init_flag = true;
		}
		else{
			$.ajax({
		    	type: "GET",
		    	dataType: "json",
		    	url: "lisbc",
		    	data: "category="+category+"&start="+page_index+"&length="+pageSize,
		        success: function(data){
		        	var returnFlag = data.returnFlag;
		        	var list = data.list;
		        	//console.log(data);
		            
		            if(returnFlag){
		            	//动态添加DOM
		                var htmlStr = "", htmlTemp = $("textarea").val();
		                
		                list.forEach(function(obj) {
		                	htmlStr += htmlTemp.temp(obj);
		                });
		                
		                //console.log(htmlStr);
		                $("#content").html(htmlStr);//将HTML插入到容器中
		            }
		        },
		        error: function(msg){
		            alert("数据加载失败！");
		        }
		    });
		}
		return false;
	};

	function loadComm(){
	    $.ajax({
	    	type: "GET",
	    	dataType: "json",
	    	url: "lisbc",
	    	data: "category="+category+"&start="+0+"&length="+pageSize,
	        success: function(data){
	        	var returnFlag = data.returnFlag;
	            //console.log(data);
	        	
	            if(returnFlag){
	            	initPager(data);
	            }
	        },
	        error: function(msg){
	        	alert("数据加载失败！");
	        }
	      });
	}
});